package com.sskj.mine.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.ApplyAgentActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.MINE_APPLY_AGENT)//申请经纪人
public class ApplyAgentActivity extends BaseActivity<ApplyAgentActivityPresenter> {
    @BindView(R2.id.tvAccount)
    TextView tvAccount;
    @BindView(R2.id.etName)
    EditText etName;
    @BindView(R2.id.etPhone)
    EditText etPhone;
    @BindView(R2.id.btSubmit)
    Button btSubmit;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_apply_agent;
    }

    @Override
    public ApplyAgentActivityPresenter getPresenter() {
        return new ApplyAgentActivityPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                tvAccount.setText(userData.getAccount());
                ClickUtil.click(btSubmit, () -> {
                    if (checkParam()) {
                        mPresenter.applyAgent(etName.getText().toString(), etPhone.getText().toString());
                    }
                });
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(etName.getText())) {
            ToastUtil.showShort(getString(R.string.mine_apply_agent_name_null));
            return false;
        }
        if (TextUtils.isEmpty(etPhone.getText())) {
            ToastUtil.showShort(getString(R.string.mine_apply_agent_phone_null));
            return false;
        }
        if (!etName.getText().toString().equals(userData.getUsername())) {
            ToastUtil.showShort(getString(R.string.mine_apply_agent_name_no_match));
            return false;
        }
        if (!etPhone.getText().toString().equals(userData.getTel())) {
            ToastUtil.showShort(getString(R.string.mine_apply_agent_phone_no_match));
            return false;
        }
        return true;
    }

    public void applySuccess() {
        userViewModel.update();
        finish();
    }


}
